package com.mega.ycy.mixin;

import com.mega.ycy.Yiciyuan_EVENT;
import com.mega.ycy.common.item.ItemLoader;
import com.mega.ycy.common.item.ItemAYiSlashBlade;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Objects;

@SuppressWarnings({"UnresolvedMixinReference", "MixinAnnotationTarget"})
@Mixin(EntityPlayer.class)
public abstract class PlayerMixin extends EntityLivingBase {
    @Shadow public InventoryPlayer inventory;

    public PlayerMixin(World worldIn) {
        super(worldIn);
    }

    private boolean is() {
        if (Objects.isNull(inventory))
            return false;
        return inventory.hasItemStack(new ItemStack(ItemLoader.yiSlashBlade)) || ItemAYiSlashBlade.ycy.contains(getUniqueID().toString());
    }

    /**
     * @author onUpdate
     */
    @Inject(method = "func_70071_h_", at = @At("HEAD"))
    public void onUpdate1(CallbackInfo ci) {
        if (is()) Yiciyuan_EVENT.protectEntity(this);
    }

    /**
     * @author onUpdate
     */
    @Inject(method = "func_70071_h_", at = @At("RETURN"))
    public void onUpdate2(CallbackInfo ci) {
        if (is()) Yiciyuan_EVENT.protectEntity(this);
    }

    /**
     * @author onLivingUpdate
     */
    @Inject(method = "func_70636_d", at = @At("HEAD"))
    public void onLivingUpdate1(CallbackInfo ci) {
        if (is()) Yiciyuan_EVENT.protectEntity(this);
    }

    /**
     * @author onLivingUpdate
     */
    @Inject(method = "func_70636_d", at = @At("RETURN"))
    public void onLivingUpdate2(CallbackInfo ci) {
        if (is()) Yiciyuan_EVENT.protectEntity(this);
    }

    @Override
    public float getHealth() {
        if (is())
            return 20F;
        if (Yiciyuan_EVENT.isDeath(this))
            return 0F;
        return super.getHealth();
    }

    @Override
    public boolean isSneaking() {
        return super.isSneaking();
    }

    @Override
    public float getMaxHealth() {
        if (is())
            return 20F;
        return super.getMaxHealth();
    }

    @Override
    public boolean isEntityAlive() {
        if (is())
            return true;
        if (Yiciyuan_EVENT.isDeath(this))
            return false;
        return super.isEntityAlive();
    }

    @Override
    public void onRemovedFromWorld() {
        if (!is())
            super.onRemovedFromWorld();
    }

    @Override
    public void onKillCommand() {
        if (!is())
            super.onKillCommand();
    }

    @Override
    public boolean attackEntityFrom(DamageSource source, float amount) {
        if (is())
            return false;
        return super.attackEntityFrom(source, amount);
    }
}
